package xiaoa.java.utils;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Random;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Collection;


/**
 * 字符串工具类
 * @author xiaoa
 * @date 2017年12月17日 下午7:14:16
 * @version V1.0
 *
 */
public class StringUtils {
	

	public static String CONTENT_TYPE_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded";
	public static String CONTENT_TYPE_TEXT_PLAIN = "text/plain";

	public static String getRandomNum(int length) {
		Random random = new Random();
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < length; i++) {
			stringBuilder.append(random.nextInt(10));
		}
		return stringBuilder.toString();
	}

	/**
	 * 校验邮箱格式
	 */
	public static boolean isEmail(String email) {
		String emailPattern = "^([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_.]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
		Pattern p = Pattern.compile(emailPattern);
		Matcher m = p.matcher(email);
		return m.matches();
	}

	/**
	 * 校验手机号码格式
	 */
	public static boolean isMobile(String mobile) {
		// String emailPattern = "1(3|4|7|5|8)[0-9]{9}$";
		String emailPattern = "^[\\d]{11}$";
		Pattern p = Pattern.compile(emailPattern);
		Matcher m = p.matcher(mobile);
		return m.matches();
	}

	/**
	 * 数字转换
	 */
	public static String num2Str(int num) {
		String str = "";
		if (num > 0) {
			if (num < 10000) {
				str = "+" + String.valueOf(num);
			} else if (num < 100000000) {
				str = "+" + String.valueOf((int) Math.floor((double) num / 10000)) + "W";
			} else {
				str = "...";
			}
		}
		return str;
	}

	/**
	 * 数字转换(没有加号)
	 */
	public static String num2Strj(int num) {
		String str = "";
		if (num > 0) {
			if (num < 10000) {
				str = String.valueOf(num);
			} else if (num < 100000000) {
				str = String.valueOf((int) Math.floor((double) num / 10000)) + "W";
			} else {
				str = "...";
			}
		}
		return str;
	}

	


	/**
	 * 百分比计
	 * 
	 * @param num
	 * @param total
	 * @return
	 */
	public static String accuracy(double num, double total) {
		DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
		// 可以设置精确几位小数
		df.setMaximumFractionDigits(0);
		// 模式 例如四舍五入
		df.setRoundingMode(RoundingMode.HALF_UP);
		if (total == 0 && num != 0) {
			return "100%";
		} else if (total == 0 && num == 0) {
			return "0%";
		}
		double accuracy_num = num / total * 100;

		Integer strbfb = Integer.valueOf(df.format(accuracy_num));
		if (strbfb > 100) {
			return "100%";
		}

		return strbfb + "%";
	}

	public static String accuracyFalse(double num, double total) {
		DecimalFormat df = (DecimalFormat) NumberFormat.getInstance();
		// 可以设置精确几位小数
		df.setMaximumFractionDigits(0);
		// 模式 例如四舍五入
		df.setRoundingMode(RoundingMode.HALF_UP);
		if (total == 0 && num != 0) {
			return "100";
		} else if (total == 0 && num == 0) {
			return "0";
		}
		double accuracy_num = num / total * 100;

		Integer strbfb = Integer.valueOf(df.format(accuracy_num));
		if (strbfb > 100) {
			return "100";
		}

		return strbfb.toString();
	}

	public static String clean(String str) {
		if (StringUtils.isEmpty(str)) {
			return "";
		}
		return str;
	}

	public static String strReplaceAll(String str) {
		return str.trim().replaceAll(" ", "+").replaceAll("%2B", "+");
	}

	/**
	 * 过滤掉Emoji表情 Emoji在mysql数据库中如果不是utf-8mb4格式会报错
	 * 
	 * @Title: filterEmojiStr
	 * @param str
	 * @return
	 * @author lkc
	 */
	public static String filterEmojiStr(String str) {
		String result = str;
		if (str != null && str.trim().length() > 0) {

			Pattern emoji = Pattern.compile(
					"[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]|[\ud852\udde5]",
					Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
			Matcher m = emoji.matcher(str);
			result = m.replaceAll("").trim();
			result = result.replaceAll("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]", "");
		}

		return result;
	}

	/**
	 * 判断字符串是否为空
	 * 
	 * @Title: isEmpty
	 * @param str
	 * @return
	 * @author lkc
	 */
	public static boolean isEmpty(String str) {
		if (str == null || str.trim().isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * 如果字符串为空返回true
	 * 
	 * @Title: isNotEmpty
	 * @param str
	 * @return
	 * @author lkc
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 将list拼接为字符串
	 * 
	 * @Title: splitJoint
	 * @param list
	 * @return
	 * @author lkc
	 */
	public static <E> String splitJoint(Collection<E> list) {

		return splitJoint(list, ",");
	}

	/**
	 * 将list拼接为字符串
	 * 
	 * @Title: splitJoint
	 * @param list
	 * @param regex
	 * @return
	 * @author lkc
	 */
	public static <E> String splitJoint(Collection<E> list, String regex) {
		if (list == null || list.isEmpty()) {
			return null;
		}
		StringBuilder sb = new StringBuilder("");
		for (E e : list) {
			if (e != null) {
				if (sb.length() == 0) {
					sb.append(e);
				} else {
					sb.append(regex).append(e.toString());
				}
			}
		}
		return sb.toString();
	}

	/**
	 * gbk转utf-8
	 * 
	 * @Title: getUTF8BytesFromGBKString
	 * @param gbkStr
	 * @return
	 * @author lkc
	 */
	public static String GBKtoUTF8(String gbkStr) {

		if (gbkStr == null || gbkStr.isEmpty()) {
			return gbkStr;
		}

		int n = gbkStr.length();
		byte[] utfBytes = new byte[3 * n];
		int k = 0;
		for (int i = 0; i < n; i++) {
			int m = gbkStr.charAt(i);
			if (m < 128 && m >= 0) {
				utfBytes[k++] = (byte) m;
				continue;
			}
			utfBytes[k++] = (byte) (0xe0 | (m >> 12));
			utfBytes[k++] = (byte) (0x80 | ((m >> 6) & 0x3f));
			utfBytes[k++] = (byte) (0x80 | (m & 0x3f));
		}
		if (k < utfBytes.length) {
			byte[] tmp = new byte[k];
			System.arraycopy(utfBytes, 0, tmp, 0, k);
			return new String(tmp);
		}
		return new String(utfBytes);
	}

	/**
	 * 拆分为list
	 * 
	 * @Title: split
	 * @param str
	 * @return
	 * @author lkc
	 */
	public static List<String> splitToList(String str) {

		if (str == null || str.isEmpty()) {
			return new ArrayList<String>();
		}

		return Arrays.asList(str.split(","));

	}

	/**
	 * 获取 ， 分割后指定位置值 0 开始
	 * 
	 * @Title: getIndex
	 * @param str
	 * @return
	 * @author lkc
	 */
	public static String getIndex(String str, int index) {

		if (str == null || str.isEmpty()) {
			return null;
		}

		String[] ss = str.split(",");

		if (ss.length <= index) {
			return null;
		}

		return ss[index];

	}

	/**
	 * 替换掉换行符
	 * @param str
	 * @return
     */
	public static String replneN(String str ){

		if (str == null || str.isEmpty()){
			return str;
		}

	   return str.replace("\r|\n", "");
	}

}
